#if defined(ARCTIC)

#if (FEATURE_MODE == 0)
/**
 @file sys_usw_xdata_api.c

 @date 2022-6-16

 @version v1.0
*/

/****************************************************************************
 *
* Header Files
*
****************************************************************************/
#include "sal.h"
#include "ctc_error.h"
#include "ctc_debug.h"
#include "ctc_spool.h"
#include "ctc_interrupt.h"
#include "ctc_warmboot.h"
#include "ctc_linklist.h"
#include "ctc_common.h"


#include "sys_usw_common.h"
#include "sys_usw_chip.h"
#include "sys_usw_wb_common.h"
#include "sys_usw_xdata.h"
#include "sys_usw_xdata_api.h"


#include "drv_api.h"


/****************************************************************************
 *
* Defines and Macros
*
*****************************************************************************/


/****************************************************************************
*
* Function
*
*****************************************************************************/

int32
sys_usw_xdata_prof_get_info(uint8 lchip, ctc_xdata_t* p_prof, uint8 with_check)
{
    sys_xdata_prof_info_t* p_prof_info = NULL;
    uint8 loop = 0,loop2 = 0;

    CTC_MAX_VALUE_CHECK(p_prof->type, CTC_XDATA_PROF_TYPE_MAX-1);
    CTC_MAX_VALUE_CHECK(p_prof->prof_id, CTC_CONST8-1);

    /* 1. entry lookup */
    p_prof_info = ctc_vector_get(p_xdata_master[lchip]->prof_vec, SYS_XDATA_ENCODE_PROF_VECTOR_IDX(p_prof->type, p_prof->prof_id));
    if (!p_prof_info)
    {
        return CTC_E_ENTRY_NOT_EXIST;
    }

    /* 2.1 get xdata prof info by sw */
    if (!with_check)
    {
        for (loop = 0; loop < p_prof_info->num; loop++)
        {
            p_prof->fields[loop].offset = p_prof_info->field[loop].src_offset;
            p_prof->fields[loop].width  = p_prof_info->field[loop].width;
        }
        p_prof->num = p_prof_info->num;
        return CTC_E_NONE;
    }

    /* 2.2 check user info and sw info */
    if (p_prof_info->num != p_prof->num)
    {
        return CTC_E_INVALID_PARAM;
    }
    for (loop2 = 0; loop2 < p_prof_info->num; loop2++)
    {
        for (loop = 0; loop < p_prof->num; loop++)
        {
            if ((p_prof_info->field[loop2].src_offset == p_prof->fields[loop].offset &&
                 p_prof_info->field[loop2].width == p_prof->fields[loop].width))
            {
                break;
            }
        }
        if (loop >= p_prof->num)
        {
           return CTC_E_INVALID_PARAM;
        }
    }

    return CTC_E_NONE;
}


int32
sys_usw_xdata_set_profile(uint8 lchip, ctc_xdata_prof_t* p_prof)
{
    SYS_XDATA_INIT_CHECK();
    SYS_XDATA_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_prof);
    SYS_XDATA_LOCK(lchip);
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_set_profile(lchip, p_prof));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}

int32
sys_usw_xdata_get_profile(uint8 lchip, ctc_xdata_prof_t* p_prof)
{
    SYS_XDATA_INIT_CHECK();
    SYS_XDATA_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_prof);
    SYS_XDATA_LOCK(lchip);
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_get_profile(lchip, p_prof));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}


int32
sys_usw_xdata_set_path(uint8 lchip, ctc_xdata_path_t* p_path)
{

    SYS_XDATA_INIT_CHECK();
    SYS_XDATA_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_path);
    SYS_XDATA_LOCK(lchip);
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_set_path(lchip, p_path));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}

int32
sys_usw_xdata_get_path(uint8 lchip, ctc_xdata_path_t* p_path)
{

    SYS_XDATA_INIT_CHECK();
    SYS_XDATA_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_path);
    SYS_XDATA_LOCK(lchip);
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_get_path(lchip, p_path));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}


int32
sys_usw_xdata_set_local(uint8 lchip, ctc_xdata_local_t* p_local)
{

    SYS_XDATA_INIT_CHECK();
    SYS_XDATA_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_local);
    SYS_XDATA_LOCK(lchip);
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_set_local(lchip, p_local));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}


int32
sys_usw_xdata_get_local(uint8 lchip, ctc_xdata_local_t* p_local)
{

    SYS_XDATA_INIT_CHECK();
    SYS_XDATA_DBG_OUT(CTC_DEBUG_LEVEL_FUNC, "%s()\n", __FUNCTION__);
    CTC_PTR_VALID_CHECK(p_local);
    SYS_XDATA_LOCK(lchip);
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_get_local(lchip, p_local));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}

int32
sys_usw_xdata_local_get_kset(uint8 lchip, ctc_xdata_local_type_t type, void* kset)
{
    sys_xdata_kset_t* p_kset = (sys_xdata_kset_t*)kset;
    SYS_XDATA_INIT_CHECK();
    CTC_MAX_VALUE_CHECK(type, CTC_XDATA_LOCAL_TYPE_MAX - 1);
    SYS_XDATA_LOCK(lchip);
    sal_memcpy(p_kset,&p_xdata_master[lchip]->kset[type], sizeof(sys_xdata_kset_t));
    SYS_XDATA_UNLOCK(lchip);

    return CTC_E_NONE;
}

int32
sys_usw_xdata_func_en(uint8 lchip, sys_xdata_func_t type, uint8 enable, void* data)
{
    SYS_XDATA_INIT_CHECK();
    if (!(CTC_WB_ENABLE(lchip) && CTC_WB_STATUS(lchip) == CTC_WB_STATUS_RELOADING))
    {
        SYS_XDATA_LOCK(lchip);
        CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_func_en(lchip, type, enable,data));
        SYS_XDATA_UNLOCK(lchip);
    }
    return CTC_E_NONE;
}

int32
sys_usw_xdata_show_solid_map(uint8 lchip, uint8 type, uint8 sub_type)
{
    SYS_XDATA_INIT_CHECK();
    CTC_USW_ERROR_RETURN_XDATA_UNLOCK(_sys_usw_xdata_show_solid_map(lchip, type, sub_type));

    return CTC_E_NONE;
}


#endif

#endif
